package com.ruoyi.web.controller.ccc3gc;

import com.ccc3gc.mysystem.VO.UserVO;
import com.ccc3gc.mysystem.bean.User;
import com.ccc3gc.mysystem.service.UserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import java.util.List;




@RestController
@RequestMapping("/api/user")
public class UserController extends BaseController{
    @Autowired
    private UserService userService;

    //用户列表
    @GetMapping("/list")
    public TableDataInfo list(User user){
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    //根据用户Id获取详情
    @GetMapping("/queryUser/{userId}")
    public AjaxResult queryUserById(@PathVariable int userId){
        UserVO userVO = userService.selectUserById(userId);
        return AjaxResult.success(userVO);
    }

    //新增用户
    @PostMapping("/adduser")
    public AjaxResult addUser(@RequestBody User user){
        int rows = userService.instertUser(user);
        if(rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //删除用户
    @DeleteMapping ("/deleteUser/{userId}")
    public AjaxResult deleteUser(@PathVariable int userId){
        int rows=userService.deleteUserById(userId);
        if (rows>0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //批量删除用户
    @DeleteMapping("/deleteUserBatch/{userIds}")
    public AjaxResult deleteUserBatch(@PathVariable List<Integer> userIds){
        int rows = userService.deleteUserByIds(userIds);
        if (rows>0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //修改用户
    @PostMapping("/updateUser")
    public AjaxResult updateUser(@RequestBody User user){
        int rows = userService.updateUser(user);
        if (rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //导出
    @PostMapping("/export")
    public void export(HttpServletResponse response,User user){
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户数据");
    }

    //导入模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.importTemplateExcel(response, "用户数据");
    }
    //导入
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        List<User> userList = util.importExcel(file.getInputStream());
        String message = userService.importUser(userList);
        return AjaxResult.success(message);
    }

    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user){
        User login = userService.userLogin(user);
        if (login.getUserId() == null) {
            return AjaxResult.error();
        }
        return AjaxResult.success(login);
    }
}

